
#include "tuya_cloud_types.h"

#include "tal_log.h"
#include "tal_queue.h"
#include "tal_thread.h"

#include "tkl_memory.h"

#include "lvgl.h"
#include "tal_api.h"

#include "lv_vendor.h"
#include "ui_application.h"
#include "disp_hub.h"
#include "logic.h"

/***********************************************************
************************macro define************************
***********************************************************/

/***********************************************************
***********************typedef define***********************
***********************************************************/
typedef struct
{
    TY_DISPLAY_TYPE_E type;
    int len;
    char *data;
} DISPLAY_MSG_T;

typedef struct
{
    QUEUE_HANDLE queue_hdl;
    THREAD_HANDLE thrd_hdl;

} TUYA_DISPLAY_T;

static TUYA_DISPLAY_T sg_display = {0};

/***********************************************************
***********************function define**********************
***********************************************************/

static void __app_display_msg_handle(DISPLAY_MSG_T *msg_data)
{
    if (msg_data == NULL)
    {
        return;
    }

    lv_vendor_disp_lock();

    lv_vendor_disp_unlock();
}

static void __disp_hub_ui_task(void *args)
{
    DISPLAY_MSG_T msg_data = {0};

    (void)args;

    lv_vendor_disp_lock();

    ui_application_init();

    lv_vendor_disp_unlock();
    PR_DEBUG("ui init success");

    for (;;)
    {
        memset(&msg_data, 0, sizeof(DISPLAY_MSG_T));
        tal_queue_fetch(sg_display.queue_hdl, &msg_data, 0xFFFFFFFF);

        __app_display_msg_handle(&msg_data);

        if (msg_data.data)
        {
            tkl_system_psram_free(msg_data.data);
        }
        msg_data.data = NULL;
    }
}

/**
 * @brief Initialize the display system
 *
 * @param None
 * @return OPERATE_RET Initialization result, OPRT_OK indicates success
 */
OPERATE_RET disp_hub_init(void)
{
    OPERATE_RET rt = OPRT_OK;

    memset(&sg_display, 0, sizeof(TUYA_DISPLAY_T));
    logic_init();

    // lvgl initialization
    lv_vendor_init(DISPLAY_NAME);
    lv_vendor_start(5, 1024 * 16);

    PR_DEBUG("lvgl init success");

    TUYA_CALL_ERR_RETURN(tal_queue_create_init(&sg_display.queue_hdl, sizeof(DISPLAY_MSG_T), 8));
    THREAD_CFG_T cfg = {
        .thrdname = "chat_ui",
        .priority = THREAD_PRIO_2,
        .stackDepth = 1024 * 32,
    };
    TUYA_CALL_ERR_RETURN(tal_thread_create_and_start(&sg_display.thrd_hdl, NULL, NULL, __disp_hub_ui_task, NULL, &cfg));
    PR_DEBUG("chat bot ui task create success");

    return rt;
}
/**
 * @brief Send display message to the display system
 *
 * @param tp Type of the display message
 * @param data Pointer to the message data
 * @param len Length of the message data
 * @return OPERATE_RET Result of sending the message, OPRT_OK indicates success
 */
OPERATE_RET app_display_send_msg(TY_DISPLAY_TYPE_E tp, uint8_t *data, int len)
{
    PR_DEBUG("TODO app_display_send_msg");
    return OPRT_OK;
    DISPLAY_MSG_T msg_data;

    msg_data.type = tp;
    msg_data.len = len;
    if (len && data != NULL)
    {
        msg_data.data = (char *)tkl_system_psram_malloc(len + 1);
        if (NULL == msg_data.data)
        {
            return OPRT_MALLOC_FAILED;
        }
        memcpy(msg_data.data, data, len);
        msg_data.data[len] = 0; //"\0"
    }
    else
    {
        msg_data.data = NULL;
    }

    tal_queue_post(sg_display.queue_hdl, &msg_data, 0xFFFFFFFF);

    return OPRT_OK;
}